import {Component, HostListener, ElementRef, Renderer, ViewContainerRef, OnInit, AfterViewInit} from '@angular/core';
import 'rxjs/add/operator/merge';
import {Router} from "@angular/router";
import {UserService} from "./user/service/user.service";
import {PersonalMessage} from "./user/entity/PersonalMessage";
import {CommonIp} from "./user/entity/Ip";
import {ActivatedRoute} from "@angular/router";
import {Params} from "@angular/router";
import 'rxjs/add/operator/switchMap';
import {CategoryFindService} from "./category/service/category.find.service";
import {Category} from "./category/entity/Category";
import {ProjectCategory} from "./category/entity/ProjectCategory";
@Component({
  selector: 'app-root',
  templateUrl: './index.html',
  styleUrls :['./app.component.css']
})
export class AppComponent extends CommonIp implements OnInit{

  public personalmessage:PersonalMessage = new PersonalMessage();
  public hide:string;
  private count:number=0;
  constructor(private router: Router,private userservice:UserService,
              public activatedRouter: ActivatedRoute) {
    super();

  }
  public navToggle(){
    $("#nav").css("display","block");

    if(this.count%2==0){
      let width = document.body.clientWidth;
      if(width<450){
       document.getElementById("content").style.display="none";
      $("#category-child-list").css("display","none");
      }
    }else{
      $("#category-child-list").css("display","");
        document.getElementById("content").style.display="";
    }
   this.count++;
  }

  isLogins(number:number){
    if(this.UserIsLogin()==null){
      this.HiddenLeftProgram();
      this.router.navigate(['/isLogin']);
      return;
    }else {
      if(number==1){
        this.router.navigate(['/personalUpdateMessage']);
        return;
      }
      if(number==2){

        sessionStorage.setItem("personInfoId",sessionStorage.getItem("id"));

        this.router.navigate(['/personal/personalFollow/',{id:sessionStorage.getItem("personInfoId")}]);

        this.hiddenNavOnPhone();
        return;
      }
      if(number==3){
        this.HiddenLeftProgram();
        sessionStorage.removeItem("email");
        sessionStorage.removeItem("name");
        sessionStorage.removeItem("id");
        sessionStorage.removeItem("userimg");

        this.router.navigate(['/isLogin']);
        return;
      }
      if(number==4){
        this.router.navigate(['/feedback']);
      }
      if(number==5){
       this.hiddenNavOnPhone();
        this.router.navigate(['/project/add']);
      }
    }
  }
  public UserIsLogin  (){
    var email=sessionStorage.getItem("email");
    return email;
  }

  private getPersonalBaseMessage(userid:string){
    this.userservice.getPersonalMessage(userid)
      .then(personalmessage=>this.personalmessage=personalmessage)
  }
  private initParam(param: any):Promise<string> {
    console.log("initParam");
    this.ShowLeftProgram();
    this.hiddenNavOnPhone();
    this.router.navigate(['/projectindexlist']);
    return param;
  }

  ngOnInit() {
    document.getElementById("preloader").style.display="none";


    if(sessionStorage.getItem("id")!=null){
      document.getElementById("loginName").innerHTML=sessionStorage.getItem("name");
      document.getElementById("IsSingOut").innerHTML="退出";

    }else {
      document.getElementById("IsSingOut").innerHTML="登陆";
    }

    if(sessionStorage.getItem("userimg")!=null){
      document.getElementById("ImgHead").removeAttribute("style");
      document.getElementById("ImgHead").setAttribute("style","height:40px;width:50px")
      document.getElementById("ImgHead").setAttribute("src",sessionStorage.getItem("userimg"));

    }
    this.activatedRouter.params.
    // (+) converts string 'id' to a number
    switchMap((params: Params) =>this.initParam(params["id"])).subscribe();

  }

}
